﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Godot.core.io
{
    public abstract class ResourceFormatLoader
    {
        public virtual ResourceInteractiveLoader load_interactive(string p_path)
        {
            //either this
            Resource res = load(p_path);
            if (res == null)
                return null;

            ResourceInteractiveLoaderDefault ril = new ResourceInteractiveLoaderDefault();
            ril.resource = res;
            return ril;
        }

        public virtual Resource load(string p_path, string p_original_path = "")
        {
            //or this must be implemented
            ResourceInteractiveLoader ril = load_interactive(p_path);
            if (ril == null)
                return null;

            ril.set_local_path(p_original_path);

            while (true)
            {

                Error err = ril.poll();

                if (err == Error.ERR_FILE_EOF)
                {
                    return ril.get_resource();
                }

                if(err != Error.OK) return null;
            }

            return null;
        }
	    
        public abstract void get_recognized_extensions(ref List<String> p_extensions);

        public virtual void get_recognized_extensions_for_type(string p_type, ref List<string> p_extensions)
        {
            if (p_type == "" || handles_type(p_type))
                get_recognized_extensions(ref p_extensions);
        }

        public bool recognize(string p_extension)
        {     
	        List<string> extensions = new List<string>();
	        get_recognized_extensions(ref extensions);
            foreach(string E in extensions)
            {
		        if (string.Compare(E, Path.GetExtension(p_extension), true) == 0)
			        return true;
	        }
	        return false;
        }

	    public abstract bool handles_type(string p_type);

	    public abstract string get_resource_type(string p_path);

        public virtual void get_dependencies(string p_path, ref List<String> p_dependencies)
        {
            //do nothing by default
        }

	    public virtual Error load_import_metadata(string p_path, ref ResourceImportMetadata r_var) 
        { 
            return Error.ERR_UNAVAILABLE; 
        }
    }
}
